Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | import { eq } from 'drizzle-orm' import { NextResponse } from 'next/server' import { withAuth } from '@/lib/auth/withAuth' import { db, schema } from '@/db' import { enterClassroom, isParentOf } from '@/lib/classroom' import { emitEntryPromptAccepted, emitEntryPromptDeclined } from '@/lib/classroom/socket-emitter' import { getUserId } from '@/lib/viewer' /** * POST /api/entry-prompts/[promptId]/respond * Respond to an entry prompt (parent only) * * Body: { action: 'accept' | 'decline' } */ export const POST = withAuth(async (request, { params }) => { try { const { promptId } = (await params) as { promptId: string } const userId = await getUserId() const body = await request.json() // Validate action if (!body.action || !['accept', 'decline'].includes(body.action)) { return NextResponse.json( { error: 'Invalid action. Must be "accept" or "decline".' }, { status: 400 } ) } // Get the prompt const prompt = await db.query.entryPrompts.findFirst({ where: eq(schema.entryPrompts.id, promptId), }) if (!prompt) { return NextResponse.json({ error: 'Prompt not found' }, { status: 404 }) } // Check if prompt is still pending if (prompt.status !== 'pending') { return NextResponse.json({ error: 'Prompt has already been responded to' }, { status: 400 }) } // Check if prompt has expired if (prompt.expiresAt < new Date()) { // Mark as expired await db .update(schema.entryPrompts) .set({ status: 'expired' }) .where(eq(schema.entryPrompts.id, promptId)) return NextResponse.json({ error: 'Prompt has expired' }, { status: 400 }) } // Verify user is a parent of the player const isParentOfPlayer = await isParentOf(userId, prompt.playerId) if (!isParentOfPlayer) { return NextResponse.json( { error: 'Not authorized. Must be a parent of the student.' }, { status: 403 } ) } // Get user info for notifications const user = await db.query.users.findFirst({ where: eq(schema.users.id, userId), }) const parentName = user?.name || 'Parent' // Get player info for notifications const player = await db.query.players.findFirst({ where: eq(schema.players.id, prompt.playerId), }) const playerName = player?.name || 'Student' // Get classroom info for notifications const classroom = await db.query.classrooms.findFirst({ where: eq(schema.classrooms.id, prompt.classroomId), }) const classroomName = classroom?.name || 'Classroom' if (body.action === 'accept') { // Update prompt status await db .update(schema.entryPrompts) .set({ status: 'accepted', respondedBy: userId, respondedAt: new Date(), }) .where(eq(schema.entryPrompts.id, promptId)) // Enter child into classroom const enterResult = await enterClassroom({ playerId: prompt.playerId, classroomId: prompt.classroomId, enteredBy: userId, }) if (!enterResult.success) { // Revert prompt status if enter failed await db .update(schema.entryPrompts) .set({ status: 'pending', respondedBy: null, respondedAt: null, }) .where(eq(schema.entryPrompts.id, promptId)) return NextResponse.json( { error: enterResult.error || 'Failed to enter classroom' }, { status: 400 } ) } // Emit socket events await emitEntryPromptAccepted( { promptId, classroomId: prompt.classroomId, classroomName, playerId: prompt.playerId, playerName, acceptedBy: parentName, }, prompt.teacherId ) return NextResponse.json({ success: true, action: 'accepted', message: `${playerName} has been entered into ${classroomName}`, }) } else { // Decline the prompt await db .update(schema.entryPrompts) .set({ status: 'declined', respondedBy: userId, respondedAt: new Date(), }) .where(eq(schema.entryPrompts.id, promptId)) // Emit socket event to teacher await emitEntryPromptDeclined( { promptId, classroomId: prompt.classroomId, playerId: prompt.playerId, playerName, declinedBy: parentName, }, prompt.teacherId ) return NextResponse.json({ success: true, action: 'declined', message: 'Entry prompt declined', }) } } catch (error) { console.error('Failed to respond to entry prompt:', error) return NextResponse.json({ error: 'Failed to respond to entry prompt' }, { status: 500 }) } }) |